#include "XpRate.h"
#include "PlayerGift.h"
#include "MySQLConnection.h"
#include "Common.h"
#include "Config.h"
#include "Chat.h"

void XpRate::init()
{   
    // clean xpRatemap
    for (map<uint64 /* character's guid */,XpRateEntry* /*xp rate */>::iterator it = this->xpRateMap.begin(); it != this->xpRateMap.end(); ++it)
    {
        XpRateEntry *entry = it->second;
        if(entry == nullptr) continue;
        delete entry;
    }
    this->xpRateMap.clear();

    // query config from DB and init;
    QueryResult result = CharacterDatabase.Query("select guid,rate,start_time,end_time,enable from kkk_levelup_xp_rate where enable = 1 ");
    if (!result) {
        LOG_WARN("kkk gift module.xprate.loading", ">> Loaded 0 gifts. DB table `kkk_levelup_xp_rate` maybe empty.");
        return;
    }
    do {
        Field *fields = result->Fetch();
        uint64 playerGuid = fields[0].Get<uint64>();
        uint32 rate = fields[1].Get<uint32>();
        bool enable = fields[4].Get<bool>();
        uint32 hours = 0;
        XpRateEntry* entry = new XpRateEntry(playerGuid,rate,hours,enable);
        this->xpRateMap[playerGuid] = entry;
    } while (result->NextRow());
}


uint32 XpRate::getXpRate(Player *p){
    uint32 rate = 100;
    uint64 playerId = p->GetGUID().GetRawValue();
    map<uint64,XpRateEntry*>::iterator it = this->xpRateMap.find(playerId);


    if(it != this->xpRateMap.end()){
        XpRateEntry* entry = it->second;
        if(entry != nullptr && entry->isEnabled()){
            rate = entry->getRate();
        }
        
    }

    //ChatHandler(p->GetSession()).SendSysMessage(std::to_string(playerId) + ":" + std::to_string(rate) );
    return rate;
}

void XpRate::setXpRate(uint64 setterGuid /*nullable*/,uint64 targetGuid /* not null*/, uint32 rate/*percentage value*/, uint32 hours, bool enabled)
{
    map<uint64,XpRateEntry*>::iterator it = this->xpRateMap.find(targetGuid);
    XpRateEntry* entry = nullptr;
    if(it != this->xpRateMap.end() ){
        XpRateEntry* entry = it->second;
    }
    if(entry == nullptr){
        entry = new XpRateEntry();
        this->xpRateMap[targetGuid] = entry;
    }
    entry->setPlayerId(targetGuid);
    entry->setEnable(enabled);
    entry->setRate(rate);
    entry->setHours(hours);

    //tbd, should be store the xp rate config into Database!!!!
}

// void XpRate::setXpRate(Player *setter /*nullable*/,Player *target /* not null*/, uint32 rate, uint32 hours, bool enabled)
// {
//     if(hours <= 0) return;
//     this->storeXpRateToDb(target,rate,hours,1);
    
//     this->refreshXpRate(target);
// }

// void XpRate::refreshXpRate(Player *target)
// {
//     XpRateEntry *entry = this->getXpRateFromDb(target);

//     if(entry != nullptr){
//         this->xpRateMap[target->GetGUID().GetEntry()] = entry->getRate();
//         delete entry;
//     }
// }

// void XpRate::storeXpRateToDb(Player *p, uint32 rate, uint32 hours, bool enabled)
// {

// }
// XpRateEntry* XpRate::getXpRateFromDb(Player *p)
// {
//     return nullptr;
// }